package com.service;

import static com.commun.Utils.ChekPrayTime;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.widget.Toast;

import com.activity.MyApplicationMainActivity;
import com.commun.Param;
import com.PrayTime.PrayTime.EnumationPrayer;

public class SilentService extends Service {

	private Handler serviceHandler = null;
	public static Boolean isServiceStarted = false;
	private Boolean threadStartSilentModeIsLaunched = false;
	private Boolean threadStopSilentModeIsLaunched = false;
	Thread threadContinue;
	Thread threadStartSilentMode;
	Thread threadStopSilentMode;

	@Override
	public void onCreate() {
		// Placez ici le code qui sera exécuté lors de la création de ce
		// service
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		isServiceStarted = true;
		serviceHandler = new Handler();
		threadContinue = new Thread(new RunTask());
		serviceHandler.postDelayed(threadContinue, 1000L);
		return START_STICKY;
	}

	@Override
	public void onDestroy() {
		if (isServiceStarted) {
			threadContinue.stop();

			serviceHandler.removeCallbacksAndMessages(threadStartSilentMode);
			serviceHandler.removeCallbacksAndMessages(threadStopSilentMode);
			if (threadStartSilentModeIsLaunched)
				threadStartSilentMode.stop();
			if (threadStopSilentModeIsLaunched)
				threadStopSilentMode.stop();
		}
	}

	@Override
	public boolean onUnbind(Intent intent) {
		backToDefaultRingerMode();
		return super.onUnbind(intent);
	}

	private final IBinder mBinder = new MonServiceSilentBinder();

	@Override
	public IBinder onBind(Intent intent) {
		return mBinder;
	}

	public class MonServiceSilentBinder extends Binder {
		public SilentService getService() {
			return SilentService.this;
		}
	}

	class RunTask implements Runnable {
		public void run() {

			EnumationPrayer prayer = ChekPrayTime();
			if (prayer != EnumationPrayer.NO_ONE)

				SetSilentModeForSalat(prayer);
			serviceHandler.postDelayed(this, 60 * 1000L);
		}
	}

	class RunTaskStartSilentMode implements Runnable {
		public void run() {
			threadStartSilentModeIsLaunched = true;
			AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
			int current = am.getRingerMode();
			PreferenceManager.getDefaultSharedPreferences(
					MyApplicationMainActivity.getmContext()).edit().putInt(
					Param.PREF_RING_MODE, current);

			PreferenceManager.getDefaultSharedPreferences(
					MyApplicationMainActivity.getmContext()).edit().commit();
			am.setRingerMode(AudioManager.RINGER_MODE_SILENT);

			int stopDelay = PreferenceManager.getDefaultSharedPreferences(
					MyApplicationMainActivity.getmContext()).getInt(
					salat + "_max", -1)
					- PreferenceManager.getDefaultSharedPreferences(
							MyApplicationMainActivity.getmContext()).getInt(
							salat + "_min", -1);

			threadStopSilentMode = new Thread(new RunTaskStopSilentMode());
			serviceHandler.postDelayed(threadStopSilentMode,
					stopDelay * 60 * 1000L);

		}
	}

	class RunTaskStopSilentMode implements Runnable {
		public void run() {
			threadStopSilentModeIsLaunched = true;
			backToDefaultRingerMode();
		}
	}

	private void backToDefaultRingerMode() {
		AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
		int back = PreferenceManager.getDefaultSharedPreferences(
				MyApplicationMainActivity.getmContext()).getInt(
				Param.PREF_RING_MODE, AudioManager.RINGER_MODE_NORMAL);
		am.setRingerMode(back);
	};

	private String salat = "";

	private void SetSilentModeForSalat(EnumationPrayer prayer) {

		switch (prayer) {
		case FAJR:
			salat = "fajr";
			break;
		case DHOHR:
			salat = "dhohr";
			break;
		case ASR:
			salat = "asr";
			break;
		case MAGHREB:
			salat = "maghreb";
			break;
		case ISHA:
			salat = "isha";
			break;
		}

		int startPeriodDelay = PreferenceManager.getDefaultSharedPreferences(
				MyApplicationMainActivity.getmContext()).getInt(salat + "_min",
				-1);
		if (startPeriodDelay > -1) {
			threadStartSilentMode = new Thread(new RunTaskStartSilentMode());
			serviceHandler.postDelayed(threadStartSilentMode,
					startPeriodDelay * 60 * 1000L);
		}

	}

}
// Toast.makeText(MyMapActivity.getmContext() , "Time :" ,
// Toast.LENGTH_LONG).show();
